package _interview150;

import _object.ListNode;

/**
 * 25. K 个一组翻转链表
 */
public class No25 {
    public ListNode reverseKGroup(ListNode head, int k) {
        // 判断长度是否小于k
        ListNode p0 = head;
        for (int i = 0; i < k; i++) {
            if (p0 == null) return head;
            p0 = p0.next;
        }
        // 反转
        ListNode pre = null, cur = head, nex = null;
        for (int i = 0; i < k; i++) {
            nex = cur.next;
            cur.next = pre;
            pre = cur;
            cur = nex;
        }
        head.next = reverseKGroup(nex, k);
        return pre;
    }
}
